package com.zpself.module.算法练习.排序;
//找规律，将结果放到不同的集合中
class 力扣_6_Z字形变换 {
    public static void main(String[] args) {
        System.out.println("LCIRETOESIIGEDHN".equals(convert("LEETCODEISHIRING",3)));
        System.out.println("LDREOEIIECIHNTSG".equals(convert("LEETCODEISHIRING",4)));
    }

    //题解:找规律，将结果放到不同的集合中
    public static String convert(String s, int numRows) {
        if (numRows==1)
            return s;
        StringBuilder[] collections = new StringBuilder[numRows];
        for (int i = 0; i < collections.length; i++)
            collections[i] = new StringBuilder();
        for (int i = 0; i < s.length(); i++){
            int index =i% (2*numRows-2);
            index = index>=numRows?2*numRows-2-index: index;
            collections[index].append(s.charAt(i));
        }
        StringBuilder result = new StringBuilder();
        for (StringBuilder collection : collections)
            result.append(collection);
        return result.toString();
    }
}